import { useUserStore } from '@/stores'
import showPrompt from '@/utils/showPrompt'
import HomeView from '@/views/Home/HomeView.vue'
import LayoutView from '@/views/layout/LayoutView.vue'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: LayoutView,
      children: [
        {
          path: '',
          component: HomeView
        },
        {
          path: 'login',
          component: () => import('@/views/login/Login.vue')
        },
        {
          path: 'register',
          component: () => import('@/views/login/Register.vue')
        },
        {
          path: 'admin-login',
          component: () => import('@/views/login/adminLogin.vue')
        },
        {
          path: 'goods-detail',
          component: () => import('@/views/goods/GoodsDetail.vue')
        },
        {
          path: 'cart',
          component: () => import('@/views/goods/ShoppingCart.vue')
        },
        {
          path: 'search',
          component: () => import('@/views/search/SearchView.vue')
        },
        {
          path: 'user',
          component: () => import('@/views/user/UserInfo.vue')
        },
        {
          path: 'order',
          component: () => import('@/views/goods/OrderList.vue')
        },
        {
          path: 'admin',
          component: () => import('@/views/admin/layout/AdminHome.vue'),
          children: [
            {
              path: '',
              component: () => import('@/views/admin/goods/GoodsManagement.vue')
            },
            {
              path: 'type',
              component: () => import('@/views/admin/type/TypeManagement.vue')
            },
            {
              path: 'order',
              component: () => import('@/views/admin/order/OrderManagement.vue')
            },
            {
              path: 'statistics',
              component: () => import('@/views/admin/statistics/OrderStatistics.vue')
            }
          ]
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/'
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0, left: 0 }
    }
  }
})

router.beforeEach(to => {
  const userStore = useUserStore()
  if (
    !userStore.userId &&
    (to.path.startsWith('/cart') ||
      to.path.startsWith('/order') ||
      to.path.startsWith('/user') ||
      to.path.startsWith('/admin')) &&
    to.path !== '/admin-login'
  ) {
    return {
      path: '/login',
      query: {
        redirect: to.path
      }
    }
  }
  if (
    userStore.userId &&
    (to.path.startsWith('/login') || to.path.startsWith('/register') || to.path === '/admin-login')
  ) {
    showPrompt('您已登录')
    return '/'
  }
  if (userStore.userId && !Number(userStore.adminPrivilege) && to.path.startsWith('/admin')) {
    showPrompt('您没有管理员权限，请登录管理员账号')
    return '/user'
  }
  return true
})

export default router
